package com.xingzhe.pengji.security.mapper.provider;

import java.util.Date;
import java.util.Map;

import com.xingzhe.pengji.framework.util.DateUtil;
import com.xingzhe.pengji.security.sql.common.SqlColumnsUtil;
import com.xingzhe.pengji.security.sql.common.SqlKeyWord;

/**
 * @Title ： 三江学院 毕业设计
 * @Description ：SqlProvider 的基础类封装一些拼接sql公用的方法
 * @author ：LuFengLiang
 * @Company: IT行者工作室
 * @Copyright: Copyright (c) 2012-2-24
 * @version ： 1.0
 * @param <T>
 */
public abstract class BaseSqlProvider<T>
{
    /**
     * 生成from table的语句
     */
    public abstract StringBuilder getFromTableString(StringBuilder sb);

    /**
     * 获取所要查询的列的
     */
    public abstract String getCommonColumnsString();

    /**
     * 使用where子句查询
     */
    public abstract StringBuilder dynamicWhereSql(StringBuilder sb, Map<String, Object> map);

    /**
     * 构建查询语句 在前面加有AND,用于where 子句的查询
     */
    public StringBuilder buildSelectStringForAnd(StringBuilder sb, String TEMP_CLOUMN, Object TEMP_VALUE,
            String TEMP_Relationship)
    {
        sb.append(SqlKeyWord.KeyWord.SPACE.getValue());
        sb.append(SqlKeyWord.KeyWord.AND.getValue());
        sb.append(SqlKeyWord.KeyWord.SPACE.getValue());
        sb.append(TEMP_CLOUMN);
        sb.append(SqlKeyWord.KeyWord.SPACE.getValue());
        if ( TEMP_Relationship.equals("LIKE") )
        {
            sb.append(SqlKeyWord.Relationship.valueOf(TEMP_Relationship).getValue());
        }
        else
        {
            sb.append(SqlKeyWord.Relationship.EQUAL.getValue());
        }
        sb = judgeTypeAndAppend(sb, TEMP_VALUE);
        return sb;
    }

    /**
     * 构建查询语句 在后面加有AND,用于update set 子句的查询
     */
    public StringBuilder buildSelectStringForSet(StringBuilder sb, String TEMP_CLOUMN, Object TEMP_VALUE)
    {
        sb.append(SqlKeyWord.KeyWord.SPACE.getValue());
        sb.append(TEMP_CLOUMN);
        sb.append(SqlKeyWord.KeyWord.SPACE.getValue());
        sb.append(SqlKeyWord.Relationship.EQUAL.getValue());
        sb = judgeTypeAndAppend(sb, TEMP_VALUE);
        sb.append(SqlKeyWord.KeyWord.SPACE.getValue());
        sb.append(SqlKeyWord.KeyWord.COMMA.getValue());
        sb.append(SqlKeyWord.KeyWord.SPACE.getValue());
        return sb;
    }

    /**
     * 判断传进值的类型生成所需要的字符串
     */
    public StringBuilder judgeTypeAndAppend(StringBuilder sb, Object TEMP_VALUE)
    {
        if ( Integer.class == TEMP_VALUE.getClass() || Long.class == TEMP_VALUE.getClass() )
        {
            sb.append(TEMP_VALUE);
        }
        else if ( String.class == TEMP_VALUE.getClass() )
        {
            sb.append(SqlKeyWord.KeyWord.SQM.getValue());
            sb.append(TEMP_VALUE);
            sb.append(SqlKeyWord.KeyWord.SQM.getValue());
        }
        else if ( Date.class == TEMP_VALUE.getClass() || TEMP_VALUE.getClass().isInstance(Date.class) )
        {
            sb.append(SqlKeyWord.KeyWord.SQM.getValue());
            sb.append(DateUtil.dateToString((Date) TEMP_VALUE, DateUtil.FORMAT_ONE));
            sb.append(SqlKeyWord.KeyWord.SQM.getValue());
        }
        return sb;
    }

    /**
     * 构建from 子句
     */
    public StringBuilder buildString(StringBuilder sb, String TABLE_NAME)
    {
        sb.append(SqlKeyWord.KeyWord.SPACE.getValue());
        sb.append(SqlKeyWord.KeyWord.FROM.getValue());
        sb.append(SqlKeyWord.KeyWord.SPACE.getValue());
        sb.append(TABLE_NAME);
        sb.append(SqlKeyWord.KeyWord.SPACE.getValue());
        return sb;
    }

    /**
     * 统计总数
     */
    public String getItemCount(Map<String, Object> map)
    {
        StringBuilder sb = new StringBuilder();
        sb.append(SqlKeyWord.KeyWord.SELECT.getValue());
        sb.append(SqlKeyWord.KeyWord.SPACE.getValue());
        sb.append(SqlKeyWord.KeyWord.COUNT.getValue());
        sb.append(SqlKeyWord.KeyWord.LEFTBRACKETS.getValue());
        sb.append(SqlKeyWord.KeyWord.START.getValue());
        sb.append(SqlKeyWord.KeyWord.RIGHTBRACKETS.getValue());
        sb = getFromTableString(sb);
        sb.append(SqlKeyWord.KeyWord.SPACE.getValue());
        sb.append(SqlColumnsUtil.STATIC_WHERE);
        sb.append(SqlKeyWord.KeyWord.SPACE.getValue());
        return sb.toString();
    }

    /**
     * 分页查询获取对象
     */
    public String queryForPaginatedList(Map<String, Object> map)
    {
        StringBuilder sb = new StringBuilder();
        sb = getSelectString(sb);
        sb = dynamicWhereSql(sb, map);
        sb = dynamicOrderBySql(sb, map);
        sb = getpageLimitSql(sb, map);
        return sb.toString();
    }

    /**
     * 生成Select 字段1,字段2....
     */
    public StringBuilder getSelectString(StringBuilder sb)
    {
        sb.append(SqlKeyWord.KeyWord.SELECT.getValue());
        sb.append(SqlKeyWord.KeyWord.SPACE.getValue());
        sb.append(getCommonColumnsString());
        sb.append(SqlKeyWord.KeyWord.SPACE.getValue());
        sb = getFromTableString(sb);
        sb.append(SqlKeyWord.KeyWord.SPACE.getValue());
        sb.append(SqlColumnsUtil.STATIC_WHERE);
        sb.append(SqlKeyWord.KeyWord.SPACE.getValue());
        return sb;
    }

    /**
     * 使用order by的排序查询
     */
    public StringBuilder dynamicOrderBySql(StringBuilder sb, Map<String, Object> map)
    {
        String orderByColumnSql = (String) map.get("orderByColumnSql");
        sb.append(SqlKeyWord.KeyWord.SPACE.getValue());
        if ( orderByColumnSql != null && orderByColumnSql.length() != 0 )
        {
            sb.append(SqlKeyWord.KeyWord.ORDER_BY.getValue());
            sb.append(SqlKeyWord.KeyWord.SPACE.getValue());
            sb.append(orderByColumnSql);
        }
        return sb;
    }

    /**
     * 使用limit关键字进行分页查询
     */
    public StringBuilder getpageLimitSql(StringBuilder sb, Map<String, Object> map)
    {
        int pageSize = (Integer) map.get("pageSize");
        int pageNumber = (Integer) map.get("pageNumber");
        sb.append(SqlKeyWord.KeyWord.SPACE.getValue());
        if ( pageSize != 0 && pageNumber >= 0 )
        {
            sb.append(SqlKeyWord.KeyWord.LIMIT.getValue());
            sb.append(SqlKeyWord.KeyWord.SPACE.getValue());
            sb.append(pageNumber);
            sb.append(SqlKeyWord.KeyWord.COMMA.getValue());
            sb.append(pageSize);
        }
        return sb;
    }
}
